<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>击鼓传花</title>
</head>

<body>
    <script>
        /* 
            队列思维
        */
        let hotPotato = function (elementList, num) {
            if (elementList.length === 0) return "ERROR";
            let eliminatedList = []; // 淘汰顺序
            while (elementList.length > 1) {
                for (let i = 0; i < num; i++) {
                    let temp = elementList.shift();
                    elementList.push(temp);
                }
                eliminatedList.push(elementList.shift());
            }
            let winner = elementList[0];
            return [winner, eliminatedList];
            /* 
                千万注意，js不能直接返回两个返回值。
            */
        }
        let elementList = ['zjl', 'ls', 'ljl', 'zl', 'zj', 'ycy', 'nym', 'zj'];
        let res = hotPotato(elementList, 5);
        console.log(res);
    </script>
</body>

</html>